using Const;
using Entitas;
using UnityEngine;

namespace Services
{
    public class AudioView:MonoBehaviour,IView,IComponentsAudioListener
    {
        private AudioSource _audioSource;
        public void Link(IEntity entity, IContext context)
        {
            GameEntity gameEntity=entity as GameEntity;
            if (gameEntity != null)
            {
                gameEntity.AddComponentsAudioListener(this);
            }
        }

        public void OnComponentsAudio(GameEntity entity, string path)
        {
            if (_audioSource == null)
            {
                _audioSource = gameObject.AddComponent<AudioSource>();
            }

            _audioSource.clip = Resources
                .Load<AudioClip>(ResPath.AudioPath + path);
            Debug.Log(ResPath.AudioPath + path);
            _audioSource.Play();
        }
    }
}